package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryProductListBean;
import com.ymatou.iapi.livebuyer.service.QueryProductListCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * 查询商品列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryProductList {
	private static QueryProductListBean queryproductlistBean;
	private static QueryProductListCall queryproductlistCall;
	private static LiveRoomWapper liveRoomWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商品列表");
	}

	@Before
	public void caseUp() {
		queryproductlistBean = new QueryProductListBean();
		queryproductlistCall = new QueryProductListCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryproductlistCall.getOpurl());
		liveRoomWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryProductList_001() {
		Logger.start(true, "查询商品列表成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);

			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);

			
			int size = 2;
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(size);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "判断code是否为200");

			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 1, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 1, "验证pageCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryProductList_002() {
		Logger.start(true, "不输入liveid失败");
		try {
			// queryproductlistBean.setLiveId(49);
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(2);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("100", queryproductlistCall.getString("code"), "判断code是否为200");
			Logger.verifyIsContains("错误的请求参数", queryproductlistCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryProductList_003() {
		Logger.start(true, "不输入页码pageIndex");
		try {
			queryproductlistBean.setLiveId(49);
			// queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(2);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("100", queryproductlistCall.getString("code"), "判断code是否为200");
			Logger.verifyIsContains("错误的请求参数", queryproductlistCall.getString("message"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryProductList_004() {
		Logger.start(true, "不输入每页条数pageSize");
		try {
			queryproductlistBean.setLiveId(49);
			queryproductlistBean.setPageIndex(1);
			// queryproductlistBean.setPageSize(2);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("100", queryproductlistCall.getString("code"), "判断code是否为200");
			Logger.verifyIsContains("错误的请求参数", queryproductlistCall.getString("message"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryProductList_005() {
		Logger.start(true, "直播未开始翻页查询商品列表成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 10;
			for (int i = 0; i < count; i++) {
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
				
			}
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			int productSid=0;
			int pageIndex = 1;
			int pagesize = 2;
			boolean hasnext = true;
			while (hasnext) {
				queryproductlistBean.setLiveId(createlive.getId());
				queryproductlistBean.setPageIndex(pageIndex);
				queryproductlistBean.setPageSize(pagesize);
				queryproductlistCall.setData(queryproductlistBean);
				queryproductlistCall.callService();
				JsonArray plist = queryproductlistCall.getProductList();
				for (JsonElement jsonElement : plist) {
					Logger.verifyEquals(ProductIdList.get(productSid),jsonElement.getAsJsonObject().get("productId").getAsString(),"验证商品排序:"+productSid);
					productSid++;
				}
				
				pageIndex++;
				if (pageIndex >= queryproductlistCall.getPageCount()) {
					hasnext = false;
				}
			}

			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 10, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 5, "验证pageCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryProductList_006() {
		Logger.start(true, "超出最大页码数查询数据为空");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);

			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);

			int size = 2;
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(100);
			queryproductlistBean.setPageSize(size);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "判断code是否为200");

			Logger.verifyEquals(1, queryproductlistCall.getTotalCount(), "验证count");
			Logger.verifyEquals(1, queryproductlistCall.getPageCount(), "验证pageCount");
			Logger.verifyEquals(null, queryproductlistCall.getProductList(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryProductList_007() {
		Logger.start(true, "直播开始翻页查询商品列表成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 10;
			for (int i = 0; i < count; i++) {
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			int productSid=0;
			int pageIndex = 1;
			int pagesize = 10;
			boolean hasnext = true;
			while (hasnext) {
				queryproductlistBean.setLiveId(createlive.getId());
				queryproductlistBean.setPageIndex(pageIndex);
				queryproductlistBean.setPageSize(pagesize);
				queryproductlistCall.setData(queryproductlistBean);
				queryproductlistCall.callService();
				JsonArray plist = queryproductlistCall.getProductList();
				for (int i=0;i<plist.size();i++) {
					Logger.verifyEquals(ProductIdList.get(productSid),plist.get(i).getAsJsonObject().get("productId").getAsString(),"验证商品排序:"+productSid);
					productSid++;
				}
				pageIndex++;
				if (pageIndex >= queryproductlistCall.getPageCount()) {
					hasnext = false;
				}
			}

			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 10, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 1, "验证pageCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryProductList_008() {
		Logger.start(true, "商品列表增加hot,newProd,videoEndTime");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Thread.sleep(2000);
			
			String pid1 =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid1);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid1);
			

			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			//求讲解
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid,3);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid,3);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid,3);
			Thread.sleep(2000);
			
			int size = 2;
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(size);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "判断code是否为200");

			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 2, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 1, "验证pageCount");

			JsonArray prods = queryproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
			
			System.out.println(pid+"---"+pid1);
			Logger.verifyEquals(false, prods.get(0).getAsJsonObject().get("hot").getAsBoolean(), "验证是否求讲解热门商品");
			Logger.verifyEquals(false, prods.get(0).getAsJsonObject().get("newProd").getAsBoolean(), "验证是否新品商品");
			Logger.verifyEquals(true, prods.get(1).getAsJsonObject().get("hot").getAsBoolean(), "验证是否求讲解热门商品");
			Logger.verifyEquals(true, prods.get(1).getAsJsonObject().get("newProd").getAsBoolean(), "验证是否新品商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryProductList_009() {
		Logger.start(true, "商品列表查询，置顶商品排第一个");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			AddSellerProductRequest addpro1 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid1 = ProductManagerCallServiceV2.addNewProduct(addpro1).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid1);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			
			Thread.sleep(2000);
			
			int size = 2;
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(size);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "判断code是否为200");
			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 2, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 1, "验证pageCount");
			
			JsonArray prods = queryproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
			
			System.out.println(pid+"---"+pid1);
			Logger.verifyEquals(false, prods.get(0).getAsJsonObject().get("top").getAsBoolean(), "验证置顶");
			Logger.verifyEquals(false, prods.get(0).getAsJsonObject().get("hot").getAsBoolean(), "验证是否求讲解热门商品");
			Logger.verifyEquals(true, prods.get(0).getAsJsonObject().get("newProd").getAsBoolean(), "验证是否新品商品");
			Logger.verifyEquals(true, prods.get(1).getAsJsonObject().get("top").getAsBoolean(), "验证置顶");
			Logger.verifyEquals(false, prods.get(1).getAsJsonObject().get("hot").getAsBoolean(), "验证是否求讲解热门商品");
			Logger.verifyEquals(false, prods.get(1).getAsJsonObject().get("newProd").getAsBoolean(), "验证是否新品商品");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryProductList_010() {
		Logger.start(true, "添加多个商品，新品只有一个");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 20;
			for (int i = 0; i < count; i++) {
//				AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
//				String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			Thread.sleep(2000);
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(50);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "判断code是否为200");
			Logger.verifyEquals(queryproductlistCall.getTotalCount(), 2, "验证count");
			Logger.verifyEquals(queryproductlistCall.getPageCount(), 1, "验证pageCount");
			
			JsonArray prods = queryproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}